function dateTimeStr(str) {
  var date = new Date(),
    year = date.getFullYear(), //年
    month = date.getMonth() + 1, //月
    day = date.getDate(), //日
    hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
    minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
    second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
  month >= 1 && month <= 9 ? (month = "0" + month) : "";
  day >= 0 && day <= 9 ? (day = "0" + day) : "";
  hour >= 0 && hour <= 9 ? hour : "";
  minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
  second >= 0 && second <= 9 ? (second = "0" + second) : "";
  if (str.indexOf('y') != -1) {
    str = str.replace('y', year)
  }
  if (str.indexOf('m') != -1) {
    str = str.replace('m', month)
  }
  if (str.indexOf('d') != -1) {
    str = str.replace('d', day)
  }
  if (str.indexOf('h') != -1) {
    str = str.replace('h', hour)
  }
  if (str.indexOf('i') != -1) {
    str = str.replace('i', minute)
  }
  if (str.indexOf('s') != -1) {
    str = str.replace('s', second)
  }
  return str;
}

//获取日期的今天，明天，后天 -1是昨天，0是今天，1是后一天
function getDateStr(today, addDayCount) {
  let date;
  if (today) {
    date = new Date(today);
  } else {
    date = new Date();
  }
  date.setDate(date.getDate() + addDayCount); //获取AddDayCount天后的日期 
  let y = date.getFullYear();
  let m = date.getMonth() + 1; //获取当前月份的日期 
  let d = date.getDate();
  if (m < 10) {
    m = '0' + m;
  };
  if (d < 10) {
    d = '0' + d;
  }
  // console.log(y + "-" + m + "-" + d)
  return y + "-" + m + "-" + d;
}

function hour(e) {
  // 判断是少一个小时还是多一个小时

  if (e < 0) {
    e = e * (-1)
    hourDown(e)
    return
  }
  return hourUp(e)
}

function hourUp(e) {
  // 比现在多几个小时
  var end = Date.now()
  var start = 1000 * 60 * 60 * e
  var interval = end + start; //结束 - 开始 = 毫秒值
  var a = new Date(interval);
  var y = a.getFullYear();
  var m = a.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  var d = a.getDate();
  d = d < 10 ? "0" + d : d;

  var h = a.getHours();
  h = h < 10 ? "0" + h : h;
  var mm = a.getMinutes();
  mm = mm < 10 ? "0" + mm : mm;
  var s = a.getSeconds();
  s = s < 10 ? "0" + s : s;
  return y + '-' + m + '-' + d + ' ' + ' ' + h + ':' + mm + ':' + s

}

function hourDown(e) {
  // 少多少个小时
  var end = Date.now()
  var start = 1000 * 60 * 60 * e
  var interval = end - start; //结束 - 开始 = 毫秒值
  var a = new Date(interval);
  var y = a.getFullYear();
  var m = a.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  var d = a.getDate();
  d = d < 10 ? "0" + d : d;

  var h = a.getHours();
  h = h < 10 ? "0" + h : h;
  var mm = a.getMinutes();
  mm = mm < 10 ? "0" + mm : mm;
  var s = a.getSeconds();
  s = s < 10 ? "0" + s : s;
  return y + '/' + m + '/' + d + '  ' + h + ':' + mm + ':' + s;
}
module.exports = {
  dateTimeStr: dateTimeStr,
  getDateStr: getDateStr,
  hour: hour
}